package com.course.db;

import java.sql.*;
import javax.servlet.http.*;


//本类实现分页功能
public class Pages extends ExecuteDB{
	ResultSet CountTopicrs = null; 
	private int intCountTopic = 0; 
	private int intPageSize;
	private int intPageCount;
	private int intPage = 1; 
	private String strSql = "";
	private String nowPage; 
	private String HttpFile;

	public Pages()
	{
		intPageSize = 3; 
	}

	// Countsql:[select count(*) from tablename]
	// request :
	public boolean InitPage(String parameters,int count,HttpServletRequest request){
		HttpFile = request.getRequestURI(); 
		HttpFile += "?";
		if(parameters != null || !parameters.equals(""))
		{
			HttpFile += parameters;
			HttpFile += "&";
		}
		//System.out.println(HttpFile);
			
		// intPage[pages]
		nowPage = request.getParameter("pages");
		if (nowPage == null) {
			intPage = 1;
		} else {
			intPage = Integer.parseInt(nowPage);
			if (intPage < 1)
				intPage = 1;
		}
		intCountTopic = count;
		
		intPageCount = (intCountTopic + intPageSize - 1) / intPageSize; 
		// 
		if (intPage > intPageCount)
		{
			intPage = intPageCount;
		}
	
		/*
		this.strSql = Countsql;
		CountTopicrs =  super.exeQuery(this.strSql);

		try{
			if (CountTopicrs.next()) {
				intCountTopic = CountTopicrs.getInt(1);
				System.out.println(intCountTopic);
			}
			intPageCount = (intCountTopic + intPageSize - 1) / intPageSize; 
			// 
			if (intPage > intPageCount)
			{
				intPage = intPageCount;
			}
			CountTopicrs.close();
		}catch(Exception e){
			e.printStackTrace();
		}*/
		
		return true;
	}

	public int getCountTopic()
	{
		return intCountTopic;
	}

	public int getPageCount() 
	{
		return intPageCount;
	}

	public int getIntPage()
	{
		return intPage;
	}

	//
	public String PageFooter() {
		String str = "";
		int next, prev;
		prev = intPage - 1;
		next = intPage + 1;
		str += "共<font color=red>" + getCountTopic() + "</font>条记录"
				+ "    共<font color=red>" + getPageCount() + "</font>页";
		str += " 第<font color=red>" + getIntPage() + "</font>页 ";
		if (intPage > 1)
			str += " <A href=" + HttpFile + "pages=1" + ">首页</A> ";
		else
			str += " 首页 ";
		if (intPage > 1)
			str += " <A href=" + HttpFile + "pages=" + prev + ">上一页</A> ";
		else
			str += " 上一页 ";
		if (intPage < intPageCount)
			str += " <A href=" + HttpFile + "pages=" + next + ">下一页</A> ";
		else
			str += " 下一页 ";
		if (intPageCount > 1 && intPage != intPageCount)
			str += " <A href=" + HttpFile + "pages=" + intPageCount
					+ ">尾页</A>";
		else
			str += " 尾页 ";

		return str;
	}

	public void close_all() {
		//db.close_all();
	}

	public int getIntPageSize() {
		return intPageSize;
	}

	public void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}

	public String getHttpFile() {
		return HttpFile;
	}

	public void setHttpFile(String httpFile) {
		HttpFile = httpFile;
	}

	public String getNowPage() {
		return nowPage;
	}

	public void setNowPage(String nowPage) {
		this.nowPage = nowPage;
	}

	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}

	public int getIntCountTopic() {
		return intCountTopic;
	}

	public void setIntCountTopic(int intCountTopic) {
		this.intCountTopic = intCountTopic;
	}

	public int getIntPageCount() {
		return intPageCount;
	}

	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}
}